﻿// HSS.Interlink.UploadFileDetail.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       UploadFileDetail.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/22/2011
// ----------------------------------------------------------------------------
namespace HSS.Interlink
{
	#region Using Directives
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Controls.Primitives;
	using System.Windows.Media;
	using System.Windows.Input;
	#endregion

	#region UploadFileDetail
	/// <summary>
	/// UploadFileDetail ChildWindow displays the details of a single upload File.
	/// </summary>
	public sealed class UploadFileDetail : ChildWindow
	{
		#region Constants
		const byte bkfocusAlpha = 207;
		#endregion

		#region Fields
		Border mainBackground;
		ButtonBase btnClose;
		TextBox txtLastException;
		bool hasError;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="viewModel">The File's ViewModel</param>
		public UploadFileDetail(UploadFileViewModel viewModel)
		{
			this.DefaultStyleKey = typeof(UploadFileDetail);
			if (null == viewModel)
				return;
			this.DataContext = viewModel;
			this.hasError = viewModel.LastException.Length > 0;
			this.KeyUp += (s, args) =>
			{
				if (args.Key == Key.Escape)
				{
					args.Handled = true;
					this.Close();
				}
			};
			this.MouseRightButtonDown += (s, e) =>
				{
					e.Handled = true;
				};
		}
		#endregion

		#region Properties
		#region Background
		/// <summary>
		/// Gets or sets the Dialog Background
		/// </summary>
		public new SolidColorBrush Background
		{
			get
			{
				return (SolidColorBrush)base.GetValue(BackgroundProperty);
			}
			set
			{
				base.SetValue(BackgroundProperty, (DependencyObject)value);
				if (null != mainBackground && null != this.Background)
				{
					RadialGradientBrush bk = mainBackground.Background as RadialGradientBrush;
					bk.GradientStops[0].Color = Color.FromArgb(bkfocusAlpha, this.Background.Color.R, this.Background.Color.G, this.Background.Color.B);
				}
			}
		}
		#endregion
		#endregion

		#region Overrides
		/// <summary>
		/// Apply the Control template
		/// </summary>
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			btnClose = this.GetTemplateChild("btnClose") as ButtonBase;
			if (null != btnClose)
			{
				btnClose.Click += (s, e) =>
				{
					this.DialogResult = true;
				};
			}

			txtLastException = this.GetTemplateChild("txtLastException") as TextBox;
			if (null != txtLastException)
				txtLastException.IsTabStop = hasError;

			mainBackground = this.GetTemplateChild("MainBackground") as Border;
			if (null != mainBackground && null != this.Background)
			{
				RadialGradientBrush bk = mainBackground.Background as RadialGradientBrush;
				bk.GradientStops[0].Color = Color.FromArgb(bkfocusAlpha, this.Background.Color.R, this.Background.Color.G, this.Background.Color.B);
			}

			this.Focus();
			this.btnClose.Focus();
		}
		#endregion
	}
	#endregion
}